//
//  SettingTableViewCell.swift
//  SitPosition
//
//  Created by apple on 2019/1/1.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


class SettingTableViewCell : UITableViewCell, Reuseable {
    
    // Icon
    lazy var iconIV: UIImageView = {
        let iv = UIImageView()
        iv.clipsToBounds = true
        iv.contentMode = UIView.ContentMode.scaleAspectFit
        return iv
    }()
    
    // 标题
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = Theme.Font.mediumFont(ofSize: 16)
        label.textColor = Theme.Color.primaryDarkTextColor
        label.lineBreakMode = .byTruncatingTail
        label.textAlignment = .left
        label.numberOfLines = 1
        label.adjustsFontSizeToFitWidth = true
        return label
    }()
    
    // 子标题
    lazy var subtitleLabel: UILabel = {
        let label = UILabel()
        label.font = Theme.Font.regularFont(ofSize: 12)
        label.textColor = Theme.Color.lighgrayTextColor
        label.lineBreakMode = .byWordWrapping
        label.textAlignment = .left
        label.numberOfLines = 0
        return label
    }()
    
    // 值标题
    lazy var valueLabel: UILabel = {
        let label = UILabel()
        label.font = Theme.Font.mediumFont(ofSize: 16)
        label.textColor = Theme.Color.primaryBlueTextColor
        label.lineBreakMode = .byTruncatingTail
        label.textAlignment = .right
        label.numberOfLines = 1
        return label
    }()
    
    // 更多指示符
    lazy var moreIndicator: UIImageView = {
        let iv = UIImageView(image: UIImage(named: "tool_more"))
        iv.clipsToBounds = true
        iv.contentMode = UIView.ContentMode.scaleAspectFit
        return iv
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifier)
        contentView.addSubviews([
            iconIV,
            titleLabel,
            subtitleLabel,
            valueLabel,
            moreIndicator
        ])
        _setupContraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


// MARK: - Private Methods

extension SettingTableViewCell {
    
    // 设置视图约束
    func _setupContraints() {
        // iconIV
        constrain(iconIV, contentView) { v, parent in
            v.top == parent.top + 33
            v.left == parent.left + 18
            v.width == 19
            v.height == 19
        }
        
        // valueLabel
        constrain(valueLabel, iconIV, contentView) { v, sibling, parent in
            v.centerY == sibling.centerY
            v.right == parent.right - 31
            v.width == 72
            v.height == 15
        }
        
        // titleLabel
        constrain(titleLabel, iconIV, valueLabel) { v, sibling, sibling2 in
            v.left == sibling.right + 12
            v.centerY == sibling.centerY
            v.height == 16
            v.right == sibling2.left - 4
        }
        
        // subtitleLabel
        constrain(subtitleLabel, titleLabel, contentView) { v, sibling, parent in
            v.top == sibling.bottom + 8
            v.left == sibling.left
            v.bottom == parent.bottom - 5
            v.right == parent.right - 13
        }
        
        // moreIndicator
        constrain(moreIndicator, iconIV, contentView) { v, sibling, parent in
            v.centerY == sibling.centerY
            v.right == parent.right - 13
            v.width == 11
            v.height == 11
        }
    }
}
